package com.lifesaftypower.util;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class HttpUtil {
	public static InputStream downloadUrl(String urlString) {
		InputStream content = null;
		try {
			HttpGet httpGet = new HttpGet(urlString);
			HttpClient httpclient = new DefaultHttpClient();
			// Execute HTTP Get Request
			HttpResponse response = httpclient.execute(httpGet);
			content = response.getEntity().getContent();
		} catch (Exception e) {
			Log.e("ERROR", e.toString());
		}
		return content;

	}

	public static UrlInfo GetUrlInfo(String urlString)
			throws MalformedURLException {
		URL url = new URL(urlString);
		UrlInfo result = new UrlInfo();
		result.set_domain(url.getHost());
		String path = url.getPath();
		int lastSlashPos = path.lastIndexOf("/");
		if (lastSlashPos > 0) {
			result.set_path(path.substring(0, lastSlashPos - 1));
			result.set_file(path.substring(lastSlashPos + 1, path.length()));
		} else {
			result.set_path("");
			result.set_file(path.substring(lastSlashPos + 1, path.length()));
		}
		return result;
	}

}
